#!usr/lib/python
# coding:utf-8
"""
@author: jeffxun
@date  : 2012-07-03
"""

import MySQLdb
import traceback
import MySQLdb.cursors

class MDBServer(object):
    """ """
    def __init__(self,(host,user,passwd,dbname),port=3306,utf="utf8",dbDict=True):
        """ """
        self._dbConn = None
        self._dbCurs = None
        self.log = None
        if self._DBConnect((host, user, passwd, dbname),port, utf, dbDict):
            pass
        else:
            raise AttributeError,"MDBServer __init__ failed"
            
    def _DBConnect(self,(host,user,passwd,dbname),port,utf,dbDict):
        """ """
        DbType = MySQLdb.cursors.DictCursor # ²éÑ¯Êý¾ÝÏÔÊ¾µÄ·½Ê½
        if not dbDict:
            DbType = MySQLdb.cursors.BaseCursor
        try:
            self._dbConn = MySQLdb.connect(host,user,passwd,dbname,port,charset=utf)
            self._dbCurs = self._dbConn.cursor(cursorclass = DbType)
        except:
            print traceback.format_exc()
            return False
        return True
    def select(self,sql):
        """ """
        try:
            self._dbConn.commit()
            self.setLog(sql)
            self._dbCurs.execute(sql)
            result = self._dbCurs.fetchall()
            return result
        except:
            err =  traceback.format_exc()
            print err
            self.setLog(err,"error")
        return None
    def insert(self,sql):
        """ """
        try:
            self.setLog(sql)
            self._dbCurs.execute(sql)
            self._dbConn.commit()
        except:
            err =  traceback.format_exc()
            print err
            self.setLog(err,"error")
            return False
        return True
    def update(self,sql):
        try:
            self.setLog(sql)
            self._dbCurs.execute(sql)
            self._dbConn.commit()
        except:
            err =  traceback.format_exc()
            print err
            self.setLog(err,"error")
            return False
        return True
    def delete(self,sql):
        try:
            self.setLog(sql)
            self._dbCurs.execute(sql)
            self._dbConn.commit()
        except:
            err =  traceback.format_exc()
            print err
            self.setLog(err,"error")
    def openLog(self,log):
        self.log = log
    def setLog(self,msg,ltype="debug"):
        """ """
        if self.log and ltype=="debug":
            self.log.debug(msg)
        elif self.log and ltype == "error":
            self.log.error(msg)
        elif self.log:
            self.log.waring(msg)
        else:
            #print msg
            pass
    def close(self):
        """ """
        self._dbConn.close()
        self._dbCurs.close()

if __name__=="__main__":
    #a = MDBServer(("127.0.0.1", "MoliKong", "123456", "molikong"),33001)
    host = "127.0.0.1"
    user = "MoliKong"
    password = "123456"
    dbname = "molikong"
    port = 33001
    MySQLdb.connect(host,user,password,dbname,port)